<?php
/**
 * Created by PhpStorm.
 * Author: sitenv@aliyun.com
 * CreateTime: 2021/07/15 9:46
 * Blog：www.myblogs.xyz
 */

namespace app\api\controller;


use app\common\model\Order;
use app\common\service\VerifyService;

class Member extends Common
{
    // 账户认证
    public function auth()
    {
        $this->require = ['name', 'card', 'front_card', 'reverse_card'];
        $this->selectable = ['address'];
        $this->verifyParam();
        $this->verifyToken();
        // 验证身份证是否允许使用
        if (!VerifyService::card($this->data['name'], $this->data['card'])){
            $this->jsonError(VerifyService::getError());
        }
        // 获取该身份证是否绑定
        $member_model = new \app\common\model\Member();
        if ($member_model->exists(['card'=>$this->data['card']])){
            $this->jsonError('该身份证已经绑定');
        }
        if (!empty($this->data['front_card'])){
            $this->data['front_card'] = $this->upload($this->data['front_card'], 'card');
        }
        if (!empty($this->data['reverse_card'])){
            $this->data['reverse_card'] = $this->upload($this->data['reverse_card'], 'card');
        }
        $rows = $member_model->operation($this->data, false);
        if (empty($rows)){
            $this->jsonError('认证失败');
        }
        // 认证信息处理
        if (!empty($this->data['name']) && !empty($this->data['card'])){
            $this->data['name'] = substr_cut($this->data['name']);
            $this->data['card'] = str_encrypt($this->data['card'], 0, mb_strlen($this->data['card'])-1);
        }
        $this->jsonResult($this->data, '认证成功');
    }

    // 我的车辆
    public function vehicle()
    {
        $this->verifyToken();
        // 获取订单信息
        $this->data['status'] = 1;
        $order_model = new Order();
        $order_model->fields = 'order_id,order_code,number,start_time,end_time,vehicle_id';
        $order_list = $order_model->getList($this->data);
        if (!empty($order_list)){
            $vehicle_model = new \app\common\model\Vehicle();
            if (is_array($order_list)){
                foreach ($order_list as $key=>&$val){
                    $address = '未知';
                    $vehicle_data = $vehicle_model->getFind($val['vehicle_id'], 'glng,glat');
                    if (!empty($vehicle_data['glng']) && !empty($vehicle_data['glat'])){
                        $address = $vehicle_model->getGpsAddress($vehicle_data['glng'].','.$vehicle_data['glat']);
                    }
                    $val['address'] = $address;
                }
            }
        }
        $this->jsonResult($order_list);
    }

    // 我的订单
    public function order()
    {
        $this->require = ['p'];
        $this->verifyParam();
        $this->verifyToken();
        // 分页
        $limit = 10; // 截取数量
        $firstRow = 0; // 开始截取
        $nowPage = empty($this->data['p'])? 1 : $this->data['p']; // 当前页码
        if ($nowPage > 1){
            $firstRow = $limit*($nowPage-1);
        }
        // 获取订单信息
        $order_model = new Order();
        $order_model->fields = 'order_id,order_code,number,start_time,end_time,vehicle_id,';
        $order_model->fields .= 'start_path,end_path,total,deposit,money,refund_money';
        $order_list = $order_model->getList($this->data, $firstRow . ',' .  $limit);
        if (!empty($order_list)){
            $vehicle_model = new \app\common\model\Vehicle();
            if (is_array($order_list)){
                foreach ($order_list as $key=>&$val){
                    $val['start_time'] = date('Y-m-d H:i', $val['start_time']);
                    $val['end_time'] = date('Y-m-d H:i', $val['end_time']);
                }
            }
        }
        $this->jsonResult($order_list);
    }

}